<?php

// MappingToCake v0.1 - Aplicación la cual genera MVC para CakePHP Orientado a servir archivos XML mediante REST
// Copyright (C) 2009  Blas Orlando Soto Muñoz
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

require("mapping/Utils.php");
require("mapping/Mapping.php");
require("mapping/FlexMapping.php");

@ $send = $_POST['send'];

if($send) {
@	$base_config = $_POST['base_config'];
	$host = $_POST['host'];
	$user = $_POST['user'];
	$password = $_POST['password'];
	$db = $_POST['db'];
@	$restful = $_POST['restful'];
@	$isSession = $_POST['isSession'];
	$mainTableSession = $_POST['mainTableSession'];
	$listClassSession = $_POST['listClassSession'];
@	$flex_ = $_POST['flex'];
	$flex_path = $_POST['flex_path'];
	$list_flex_views = explode(",",$_POST['listViews']);
	$cake_name = $_POST['cake_name'];
	$security_salt = $_POST['security_salt'];
	if($_POST['listTables'] != null) {
		$listTablesNoProtected = explode(",",$_POST['listTables']);
		$tables = explode(",",$_POST['listTables'].",".$listClassSession.",".$mainTableSession);
	}
	else {
		$listTablesNoProtected = null;
		$tables = explode(",",$listClassSession.",".$mainTableSession);
	}
	
	@ $db_instance = mysql_connect($host,$user,$password) or message_error('Error de Conexion a MySQL');
	mysql_select_db($db);
	
	$mapping = new Mapping();
	$flex = new FlexMapping();
	echo "<link rel='stylesheet' type='text/css' href='mapping/style.css'>";
	echo "<div id='title'>";
	echo "<h1>Mapping to CakePHP - Generating Code</h1>";
	echo "</div>";
	echo "<div id='bodywrap'>";
	if($base_config) {
	  echo "<h3>CackePHP Base Config</h3>";
	  echo "Generating Base Config";
	  $mapping->configuraDatabase($host,$user,$password,$db);
	  $mapping->configuraRutas($tables);
	  $mapping->configuraSalidaDebug($security_salt);
	  echo " [<span style=\"color: rgb(51, 255, 51);\">OK</span>]<br>";
	}
  
	if($restful) {  
	  echo "<h3>RESTful</h3>";
	  echo "Generating the Class Model";
	  $mapping->model($tables);
	  echo " [<span style=\"color: rgb(51, 255, 51);\">OK</span>]<br>";
	  echo "Generating the Class Controller";
	  if($isSession) {
		  $mapping->controllerWithSession($mainTableSession,explode(',',$listClassSession));
		  $tablesNoSession = $mapping->getTablesNoSession($tables,$mainTableSession,$listClassSession);
		  $mapping->controller($tablesNoSession);
	  }
	  else {	
		  $mapping->controller($listTablesNoProtected);
	  }
	  echo " [<span style=\"color: rgb(51, 255, 51);\">OK</span>]<br>";
	  echo "Generating the Views";
	  if($isSession) {
		  $mapping->viewWithSession($tables);
	  }
	  else {
		  $mapping->view($tables);
	  }
	  echo " [<span style=\"color: rgb(51, 255, 51);\">OK</span>]<br><br>";
	}
	if($flex_) {
		echo "<h3>Flex Builder</h3>";
		echo "Generating Flex Views";
		
		$flex->createMediator($list_flex_views,$flex_path,$cake_name);
		$flex->createComponent($list_flex_views,$flex_path,$cake_name);
		
		echo " [<span style=\"color: rgb(51, 255, 51);\">OK</span>]<br><br>";
	}
	
	echo "<h3>Satisfactory mapping <a href=\"javascript:back();\">Back</a></h3><br>";
	echo "</div>";
}
else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <script type="text/javascript" src="mapping/jquery-1.4.2.js"></script>
  <script type="text/javascript">
	$(document).ready( function() {
		$(".notice").hide();
		$("#doLoadTables").hide();
		$(".ckecked_baseconfig").hide();
		$(".ckecked_restuful").hide();
		$(".ckecked_session").hide();
		$(".checked_flexview").hide();
		$("#tdProtectedSession").hide();
		$("#turn_right").hide();
		$("#all_turn_right").hide();
		$(".notice").fadeIn();
		
		$("#base_config").click( function () {
			if(this.checked)
				$(".ckecked_baseconfig").fadeIn();
			else
				$(".ckecked_baseconfig").fadeOut();
		});
		
		$("#restful").click( function () {
			if(this.checked) {
				$(".ckecked_restuful").fadeIn();
				$(".ckecked_session").fadeIn();
				$("#doLoadTables").fadeIn();
			}
			else {
				$(".ckecked_restuful").fadeOut();
				$(".ckecked_session").fadeOut();
				$("#doLoadTables").fadeOut();
			}
		});
		
		$("#session").click( function () {
			if(this.checked) {
				$("#tdProtectedSession").fadeIn();
				$("#turn_right").fadeIn();
				$("#all_turn_right").fadeIn();
			}
			else {
				$("#tdProtectedSession").fadeOut();
				$("#turn_right").fadeOut();
				$("#all_turn_right").fadeOut();
			}
		});
		
		$("#flexview").click( function () {
			if(this.checked)
				$(".checked_flexview").fadeIn();
			else
				$(".checked_flexview").fadeOut();
		});
		
		function listProtectedToString() {
			var foo ="";
			var flag = true;
			$.each($("#list_classes_protected option"), function(i,item) {
				if(flag) {
					foo = item.text;
					flag = false;
				}
				else
					foo = foo + "," + item.text;
			});
			$("#listClassSession").val(foo);
		}
		
		function listViewsToString() {
			var foo ="";
			var flag = true;
			$.each($("#list_views_selectd option"), function(i,item) {
				if(flag) {
					foo = item.text;
					flag = false;
				}
				else
					foo = foo + "," + item.text;
			});
			$("#listViews").val(foo);
		}
		
				
		function listClassesToString() {
			var foo ="";
			var flag = true;
			$.each($("#list_classes option"), function(i,item) {
				if(flag) {
					foo = item.text;
					flag = false;
				}
				else
					foo = foo + "," + item.text;	
			});
			$("#listTables").val(foo);
		}
		
				
		$("#turn_right").click( function() {
			$('#list_classes option:selected').remove()
											  .appendTo("#list_classes_protected");
			listProtectedToString();
			listClassesToString();
		});
		
		$("#all_turn_right").click( function() {
			$('#list_classes option').remove()
											  .appendTo("#list_classes_protected");
			listProtectedToString();
			listClassesToString();
		});
		
		$("#turn_left_to_classes").click( function() {
			$('#list_classes_protected option:selected').remove()
											  .appendTo("#list_classes");
			listProtectedToString();
			listClassesToString();
		});
		
		$("#all_turn_left_to_classes").click( function() {
			$('#list_classes_protected option').remove()
											  .appendTo("#list_classes");
			listProtectedToString();
			listClassesToString();
		});
		
		$("#turn_right_views").click( function() {
			$('#list_views option:selected').remove()
											  .appendTo("#list_views_selectd");
			listViewsToString();
		});
		
		$("#all_turn_right_views").click( function() {
			$('#list_views option').remove()
											  .appendTo("#list_views_selectd");
			listViewsToString();
		});
		
		$("#turn_left_views").click( function() {
			$('#list_views_selectd option:selected').remove()
											  .appendTo("#list_views");
			listViewsToString();
		});
		
		$("#all_turn_left_views").click( function() {
			$('#list_views_selectd option').remove()
											  .appendTo("#list_views");
			listViewsToString();
		});
		
		$("#exclude").click( function() {
			$('#list_classes option:selected').remove()
											  .appendTo("#list_exclude");
			listClassesToString();
		});
		
		$("#include").click( function() {
			$('#list_exclude option:selected').remove()
											  .appendTo("#list_classes");
			listClassesToString();
		});
				
		$("#turn_left").click( function() {
			if($("#main_class_login").val() != "")
				$("#list_classes").append($("<option>"+$("#main_class_login").val()+"</option>"));
			$("#main_class_login").val($('#list_classes option:selected').val());
			$('#list_classes option:selected').remove();
			listClassesToString();
		});
		
		$("#main_turn_right").click( function() {
			if($("#main_class_login").val() != "") {
				$("#list_classes").append($("<option>"+$("#main_class_login").val()+"</option>"));
				$("#main_class_login").val("")
			}
			listClassesToString();
		});
		
		$("#doLoadTables").click( function () {
			$.post("mapping/loadTables.php", { user: $("#user").val(), password: $("#password").val(), host: $("#host").val(), db: $("#db").val()},
			   function(data){
					$.each(data.items, function(i,item) {
						$("<option>").attr("value",item)
									 .html(item + "</option>")
									 .appendTo("#list_classes");
						$("<option>").attr("value",item)
									 .html(item + "</option>")
									 .appendTo("#list_views");
					});
					listClassesToString();
			   },"json");
		});
	});
  </script>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>Mapping to CakePHP v3.0</title>
  <link rel="stylesheet" type="text/css" href="mapping/style.css">
</head>
<body class="general">
<div id="title">
	<h1>Mapping to CakePHP v3.0</h1>
</div>
<div id="bodywrap">
<form class="formulario" method="post" action="mapping.php" name="send_data">
  <table class="tabla" cellspacing="2">
    <tbody>
      <tr>
        <td>Host</td>
        <td><input name="host" value="localhost" id="host"></td>
      </tr>
      <tr>
        <td>User</td>
        <td><input name="user" value="root" id="user"></td>
      </tr>
      <tr>
        <td>Password</td>
        <td><input name="password" type="password" id="password"></td>
      </tr>
      <tr>
        <td>Data Base</td>
        <td><input name="db" id="db">
        <input id="doLoadTables" type="button" name="button" value="Load Database" class="button"></td>
      </tr>
      <tr>
	<td>Generate CackePHP Base Config</td>
	<td><INPUT type="checkbox" name="base_config" id="base_config"></td>
      </tr>
      <tr class="ckecked_baseconfig">
        <td>Security.salt</td>
        <td><input name="security_salt"></td>
      </tr>
      <tr>
        <td>Generate CackePHP RESTful</td>
        <td><INPUT type="checkbox" name="restful" id="restful"></td>
      </tr>
      <tr class="ckecked_restuful">
        <td>Generate CackePHP Sessions</td>
        <td><input name="isSession" type="checkbox" id="session"><br>
        </td>
      </tr>
    </tbody>
  </table>
  <div class="ckecked_restuful">
  <div class="ckecked_session">
  <table cellspacing="2" class="tabla">
    <tbody>
      <tr>
        <td>
        	Main Class Login Sessions <br /><input name="mainTableSession" READONLY id="main_class_login" /><br />
            <input type="button" value=">" id="main_turn_right"/><br />
        </td>
        <td>
        	List Classes<br />
            <select name="select" multiple="multiple" id="list_classes" size="10">
            </select><br />
            <input type="button" value=">" id="turn_right" /><br />
            <input type="button" value=">>" id="all_turn_right" /><br />
            <input type="button" value="<" id="turn_left" /><br />
            <input type="button" value="Exclude" id="exclude" /><br />
            List Exclude<br />
            <select name="select" multiple="multiple" id="list_exclude" size="10">
            </select><br />
            <input type="button" value="Include" id="include" /><br />
            <input type="hidden" id="listTables" name="listTables"/>
        </td>
        <td id="tdProtectedSession">
        	Classes Protected by Session<br />
            <select name="select" id="list_classes_protected" multiple="multiple" size="10">
            </select><br />
            <input type="button" value="<" id="turn_left_to_classes" /><br />
            <input type="button" value="<<" id="all_turn_left_to_classes" /><br />
            <input type="hidden" name="listClassSession" id="listClassSession"/>
        </td>
      </tr>
    </tbody>
  </table>
  </div>
  </div>
  <table cellspacing="2" class="tabla">
    <tbody>
      <tr>
        <td width="276">Generate Flex View</td>
        <td width="262"><input name="flex" type="checkbox" id="flexview"></td>
      </tr>
      <tr class="checked_flexview">
        <td>Project CakePHP Name</td>
        <td><input name="cake_name"></td>
      </tr>
      <tr class="checked_flexview">
        <td>Flex Builder Project Path</td>
        <td><input name="flex_path"></td>
      </tr>
      <tr class="checked_flexview">
        <td>Select Views</td>
        <td>
        	<table cellspacing="2">
            	<tbody>
                	<tr>
                    	<td>
                        	List Views<br />
                            <select name="select" multiple="multiple" id="list_views" size="10">
                            </select><br />
                            <input type="button" value=">" id="turn_right_views" /><br />
                            <input type="button" value=">>" id="all_turn_right_views" /><br />
                        </td>
                        <td>
                        	List Selected Views<br />
                        	<select name="select" multiple="multiple" id="list_views_selectd" size="10">
                            </select><br />
                            <input type="button" value="<" id="turn_left_views" /><br />
                            <input type="button" value="<<" id="all_turn_left_views" /><br />
                            <input type="hidden" name="listViews" id="listViews"/>
                        </td>
                    </tr>
                </tbody>
            </table>
        </td>
      </tr>
    </tbody>
  </table>
  <p>
    <input name="send" value="Load" type="submit" class="button">
    <br>
  </p>
</form>
</div>
<div class="notice" >
***NOTICE: When executing
this script, get modified and lost the contents of the
files app/config/routes.php - app/config/core.php and the
app/config/database.php.
</div>
</body>
</html>

<?php } ?>
